package com.gulimall.product.controller;

import com.common.utils.PageUtils;
import com.common.utils.R;
import com.gulimall.product.entity.AttrEntity;
import com.gulimall.product.entity.ProductAttrValueEntity;
import com.gulimall.product.service.AttrService;
import com.gulimall.product.service.ProductAttrValueService;
import com.gulimall.product.vo.AttrRespVo;
import com.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author myf
 * @email myf@gmail.com
 * @date 2024-06-27 19:42:14
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 更新商品规格
     */
    @PostMapping("/update/{spuId}")
    //@RequiresPermissions("product:attr:list")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities){

        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /**
     * 查询商品规格
     */
    @GetMapping("/base/listforspu/{spuId}")
    //@RequiresPermissions("product:attr:list")
    public R baseAttrListForspu(@PathVariable("spuId") Long spuId){

        List<ProductAttrValueEntity> data = productAttrValueService.baseAttrListForspu(spuId);

        return R.ok().put("data", data);
    }


    @RequestMapping("/{attrType}/list/{categoryId}")
    public R baseAttrList(@RequestParam Map<String, Object> params,@PathVariable("categoryId") Long categoryId,@PathVariable("attrType") String attrType ){

        PageUtils page = attrService.queryBaseAttrPage(params,categoryId,attrType);
        return R.ok().put("page",page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
//		AttrEntity attr = attrService.getById(attrId);
        AttrRespVo attr = attrService.getAttrInfo(attrId);
        return R.ok().put("attr", attr);
    }
    /**
     * 信息
     */
    @RequestMapping("/infoName/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public String infoName(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);
        String attrName = attr.getAttrName();
        return attrName;
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrRespVo attr){
//		attrService.updateById(attr);
		attrService.updateAttrRespVo(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
